package com.tg.app.playback;

import android.content.Context;

import com.tg.app.camera.Camera;
import com.tg.app.statistics.TangeVideoPlayStatistics;
import com.tg.appcommon.android.TGLog;
import com.tg.data.http.entity.EventMessageBean;
import com.tange.base.toolkit.DateUtil;
import com.demo.view.TimeRuleView;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Describe:
 * Created by Rance Rang on 2019-09-15.
 * <p>
 * Copyright (c) Tange.Inc All rights reserved.
 */
public abstract class TGPlayback {
    private static final String TAG = "TGPlayback";
    protected Camera camera;
    private WeakReference<Context> context;
    public TimeZone timeZone;
    public String day;
    public boolean isPauseAudio;
    private boolean isStop,isPause;
    protected int speed = 1;
    protected boolean isWifiDevice;
    public PlaybackMediaListener playbackMediaListener;
    protected List<TimeRuleView.TimePart> timeRuleList = new CopyOnWriteArrayList<>();//
    private ArrayList<TimeRuleView.TimePart> recordTimes = new ArrayList<>();
    private List<EventMessageBean> eventList = new ArrayList<>();

    protected boolean playbackRecordCalledBefore = false;

    public boolean isPlaybackRecordCalledBefore() {
        return playbackRecordCalledBefore;
    }

    public void playbackRecord(long time) {
        TangeVideoPlayStatistics.resetStartTimePlayBackTotal();
        playbackRecordCalledBefore = true;
        isStop = false;
        isPause = false;
    }

    public void setWifiDevice(boolean wifiDevice) {
        isWifiDevice = wifiDevice;
    }

    public abstract void startAudio();

    public abstract void stopAudio();

    public abstract void stopShow();

    public void stop() {
        isStop = true;
    }

    public boolean isStop() {
        return isStop;
    }

    public abstract void clear();

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public int getSpeed(){
        return speed;
    }

    public List<TimeRuleView.TimePart> getTimeRuleList() {
        return timeRuleList;
    }

    public void destroy() {
        eventClear();
    }
    protected void  eventClear(){
        timeRuleList.clear();
        recordTimes.clear();
        eventList.clear();
    }


    public List<EventMessageBean> getEventList() {
        return eventList;
    }

    public synchronized ArrayList<TimeRuleView.TimePart> getRecordTimes() {
        return recordTimes;
    }

    public TGPlayback(Context context, Camera camera) {
        this.context = new WeakReference<>(context);
        this.camera = camera;
    }

    public Context getContext() {
        return context.get();
    }


    public void setTimeZone(TimeZone timeZone) {
        this.timeZone = timeZone;
    }

    public void setPlaybackMediaListener(PlaybackMediaListener playbackMediaListener) {
        this.playbackMediaListener = playbackMediaListener;
    }


    public void getRecordList(String day) {
        this.day = day;
        TGLog.trace("getRecordList " + day);
        eventClear();
    }

    /**
     * 合并区间
     */
    public void timeListMerge() {
        if (recordTimes.size() == 0) return;
        TimeRuleView.TimePart timePart = recordTimes.get(0);
        for (int i = 1; i < recordTimes.size(); i++) {
            TimeRuleView.TimePart part = recordTimes.get(i);
            if (part!=null){
                if (part.startTime <= timePart.endTime) {
                    timePart.endTime = Math.max(timePart.endTime, part.endTime);
                } else {
                    timeRuleList.add(timePart);
                    timePart = part;
                }
            }
        }
        timeRuleList.add(timePart);
    }

    /**
     * 当前时间点是否在区间内
     *
     * @param time
     * @return
     */
    public boolean isContainsTime(int time) {
        boolean has = false;
        if (timeRuleList.size() == 0) return false;
        for (TimeRuleView.TimePart timePart : this.timeRuleList) {
            if (timePart.startTime <= time && time <= timePart.endTime) {
                has = true;
                break;
            }
        }
        return has;
    }

    public String getPartOssId(int time) {
        String ossId = null;
        if (timeRuleList.size() == 0) return ossId;
        for (TimeRuleView.TimePart timePart : this.timeRuleList) {
//            LogUtils.d("timePart ossId :%s %d ,%d ",timePart.ossId,DateUtil.getTimeFiveSec(timePart.startTime),time);
            if (DateUtil.getTimeFiveSec(timePart.startTime) - 5 <= time && time <= timePart.endTime) {
                ossId = timePart.ossId;
                break;
            } else if (time < timePart.startTime) {
                break;
            }

        }
        return ossId;
    }

    /**
     * 获取下一个块的开始时间点
     *
     * @param pTime
     * @return
     */
    public int getNextPartStartTime(int pTime) {
        int time = -1;
        for (TimeRuleView.TimePart timePart : this.timeRuleList) {
            if (pTime < timePart.startTime) {
                time = timePart.startTime;
                break;
            }
        }
        if (time > 0) {
            int fTime = DateUtil.getTimeFiveSec(time);
            if (DateUtil.getTimeFiveSec(pTime) == fTime) {
                time = fTime + 5;//对五取整时相同，则加5s
            }
            TGLog.d(TAG, "getNextPartStartTime  p:%s time:%s", DateUtil.formatTimeHHmmss(pTime), DateUtil.formatTimeHHmmss(time));
        }
        return time;
    }

    public  void  onPause(){
        isPause = true;
    }

    protected boolean isPause() {
        return isPause;
    }

    public void onResume() {
        isPause = false;
    }

}
